#!/bin/bash
#
# The purpose of this script is to find APT output photometry files
# in a certain folder, and run the reunification script to
# zip their photometry together.  The script will keep only those
# records that are common to all the channels for a given target.
# 
# Assumptions:
#  1. All the files are named *_irac[1234].tbl.
#  2. The output from the reunification script is unified.out.
#
# Author:
#  Mark Abajian               (626) 395-1811
#  Infrared Processing and Analysis Center
#  California Institute of Technology
#  Pasadena, California
#

script_basename=`basename $0`

function usage() {
  echo "Usage: $script_basename"
}

if [ $# -ge 1 ] ; then
    usage
    exit 1
fi

dir=../Photometry/NITARP-TRS1

for channel1 in $dir/*_irac1.tbl ; do
    object=`basename $channel1 _irac1.tbl`

    # Run the reunification script to zip the photometry together
    echo reunification.py $dir/${object}_irac[12].tbl
    ./reunification.py $dir/${object}_irac{1,2}.tbl
    if [ $? -ne 0 ] ; then
	echo "Error:  Some problem running reunification.py on $object"
	exit 1
    fi

    # Rename the output file to match the target.
    mv unified.out $dir/${object}_irac12.csv
    if [ $? -ne 0 ] ; then
	echo "Error:  Some problem getting results file for $object"
	exit 1
    fi
done
